﻿window.onload = function()
{
    //所有代码都在页面完全加载之后执行
    //先加载所有输入框对象
    var question1_select = document.getElementById("question1_select");
    var question1_custom_text = document.getElementById("question1_custom_text");
    var answer1_text = document.getElementById("answer1_text");
    var question2_select = document.getElementById("question2_select");
    var question2_custom_text = document.getElementById("question2_custom_text");
    var answer2_text = document.getElementById("answer2_text");
    var email_text = document.getElementById("email_text");

    var notification = document.getElementById("notification");

    var sendEmail = document.getElementById("sendEmail");
    
    //声明存储邮箱地址有效性的变量
    var emailCheckResult = false;

    //加载按钮，指定按钮按下之后开始检查有效性
    sendEmail.onclick = function()
    {
        //检查各个输入框是否为空
        if (question1_select.selectedIndex == 0)
        {
            notification.innerHTML = "您还没有设置问题 1";
            return false;
        }
        else if (question1_select.selectedIndex == 12 && question1_custom_text.value.trim() == "")
        {
            notification.innerHTML = "您没有设置自定义问题 1";
            return false;
        }
        else if (answer1_text.value.trim() == "")
        {
            notification.innerHTML = "您没有设置问题 1 的答案";
            return false;
        }
        else if (question2_select.selectedIndex == 0)
        {
            notification.innerHTML = "您没有设置问题 2";
            return false;
        }
        else if (question2_select.selectedIndex == 12 && question2_custom_text.value.trim() == "")
        {
            notification.innerHTML = "您没有设置自定义问题 2";
            return false;
        }
        else if (answer2_text.value.trim() == "")
        {
            notification.innerHTML = "您没有设置问题 2 的答案";
            return false;
        }
        else if (email_text.value.trim() == "")
        {
            notification.innerHTML = "您没有填写安全邮箱";
            return false;
        }
        else if (emailCheckResult == false)
        {
            notification.innerHTML = "您输入的邮箱地址不合法";
            return false;
        }
        return true;
    };
    
    //检查邮箱地址合法性
    //emailCheckResult只用来检查有效性，而非非空
    email_text.onkeyup = function()
    {
        var pattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        if (pattern.test(email_text.value.trim()))
        {
            emailCheckResult = true;
            email_text.style.backgroundColor = "#aeffae";
        }
        else
        {
            emailCheckResult = false;
            email_text.style.backgroundColor = "#ffb1b1";
        }
    };
    
    //设置自定义问题输入框的动画
    //问题1
    $(function()
    {

        function runShowEffect()
        {
            $("#question1_custom_div").show("blind", {}, 500);
        }

        function runHideEffect()
        {
            $("#question1_custom_div").hide("blind", {}, 500);
        }

        $("#question1_select").change(function()
        {
            if ($("#question1_select").val() == "12")
            {
                runShowEffect();
            }
            else
            {
                runHideEffect();
            }
        });
    });
    //问题2
    $(function ()
    {

        function runShowEffect()
        {
            $("#question2_custom_div").show("blind", {}, 500);
        }

        function runHideEffect()
        {
            $("#question2_custom_div").hide("blind", {}, 500);
        }

        $("#question2_select").change(function ()
        {
            if ($("#question2_select").val() == "12")
            {
                runShowEffect();
            }
            else
            {
                runHideEffect();
            }
        });
    });
}